﻿Shader "Custom/Vignette"
{
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_Color("Color", Color) = (0, 0, 0, 1)
		_Radius("Radius", Range(0, 1)) = 0.5
		_Softness("Softness", Range(0, 1)) = 0.5
	}

		SubShader
		{
			Tags { "RenderType" = "Opaque" }
			Pass
			{
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"

				struct appdata
				{
					float4 vertex : POSITION;
					float2 uv : TEXCOORD0;
				};

				struct v2f
				{
					float2 uv : TEXCOORD0;
					float4 vertex : SV_POSITION;
				};

				sampler2D _MainTex;
				fixed4 _Color;
				float _Radius;
				float _Softness;

				v2f vert(appdata v)
				{
					v2f o;
					o.vertex = UnityObjectToClipPos(v.vertex);
					o.uv = v.uv;
					return o;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					// 计算当前像素到屏幕中心的距离
					float2 centerUV = float2(0.5, 0.5);
					float dist = distance(i.uv, centerUV);

					// 计算暗角强度和alpha值
					float vignetteStrength = smoothstep(_Radius, _Radius + _Softness, dist);
					float alpha = lerp(1.0, vignetteStrength, _Softness);

					// 混合原始颜色和暗角颜色
					fixed4 col = tex2D(_MainTex, i.uv);
					col.rgb = lerp(col.rgb, _Color.rgb, _Color.a * alpha);
					col.a *= alpha;

					return col;
				}
				ENDCG
			}
		}
}
